import { useReducer } from "react";
const App = () => {
  // reducer 是一个计算的函数
  // 每次使用dispath的时候,触发reducer函数
  // state表示上一次的值,action是本地的动作
  const reducer = (state, action) => {
    //state不能直接修改,必须返回一个新值
    switch (action.type) {
      case "add":
        return action.value;
      case "reduce":
        return state - 1;
      default:
        return state;
    }
  };
  const [count, dispach] = useReducer(reducer, 0);
  return (
    <div>
      <p>{count}</p>
      <button onClick={() => dispach({ type: "add", value: count + 1 })}>
        累加
      </button>
      <button onClick={() => dispach({ type: "reduce", value: count - 1 })}>
        累减
      </button>
    </div>
  );
};
export default App;
